package com.niu.business.utils.ioUtils;


import com.niu.business.utils.date.DateUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class IoWriteUtil {

//    @Async("doSomeThingExecutor")
    public void writeTxt(String path){
        File file = new File(path);
        FileOutputStream outputStream = null;
        String startDate = DateUtils.formatDate(new Date());
        System.out.println(startDate);
        try {
            if (!file.exists()){
                file.createNewFile();
            }
            outputStream = new FileOutputStream(file);
            for (int i = 0; i < 20000000; i++) {
                String str = "AEF"+i+"0<!>EFN"+i+1+"0<!>FBG30<!>GNG"+i+2+"0<!>JIS"+i+3+"0<!>JIS"+i+4+
                        "0<!>JID"+i+5+"0<!>YYR"+i+6+"0<!>ASO"+i+7+"0<!>ASD"+i+8+"0<!>" + "\n";
                outputStream.write(str.getBytes("utf-8"));
            }
        }catch (IOException e){
            e.fillInStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(DateUtils.formatDate(new Date()));
    }
    public void writeTxt(String path,Map map){
        File file = new File(path);
        FileOutputStream outputStream = null;
        String startDate = DateUtils.formatDate(new Date());
        System.out.println(startDate);
        try {
            if (!file.exists()){
                file.createNewFile();
            }
            outputStream = new FileOutputStream(file);
            for (int i = 0; i < 20000000; i++) {
                String str = (String) map.get(i);
                outputStream.write(str.getBytes("utf-8"));
            }
        }catch (IOException e){
            e.fillInStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(DateUtils.formatDate(new Date()));
    }

    public Map readTxt(String path){
        File file = new File(path);
        Map map = new HashMap();
            FileReader fileReader = null;
        String startDate = DateUtils.formatDate(new Date());
        try {

            fileReader = new FileReader(path);
            BufferedReader reader = new BufferedReader(fileReader);
            String str = "";
            int num = 0;
            while (reader.readLine() != null){
                num++;
                str = reader.readLine();
                String[] split = str.split("<!>");

                map.put(num,split);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(DateUtils.formatDate(new Date()));
        return map;
    }


}

